<?php
include("/var/www/html/new/d/common/auth.php");
include("/var/www/html/new/d/common/errBack.php");
include("/var/www/html/new/d/common/err_code.php");
include("/var/www/html/new/d/class/ProductInfo.php");
include("/var/www/html/new/d/class/Member.php");
include("/var/www/html/new/d/class/DisOrder.php");
include("/var/www/html/new/d/class/abc/DisOrder_H.php");
include("/var/www/html/new/d/class/abc/Product_H.class.php");
include("/var/www/html/new/d/class/Sale.class.php");
include("/var/www/html/new/d/module/common/Db.class.php");

$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));


if(count($_REQUEST['c_area'])){// 演出类产品数据处理
	$dbConf = include '/var/www/html/new/d/module/common/db.conf.php';// 远端服务器配置信息
	\PFT\Db::Conf($dbConf['remote_1']);
	$_REQUEST['c_pids'] = $c_pids = array_filter($_REQUEST['c_pids']);
	$_REQUEST['pid']    = $pid    = key($c_pids);
	$_REQUEST['tnum']   = $tnum   = current($c_pids);
	
	// 组合生成座位依据
	$seat_data = array();
	foreach($_REQUEST['c_pids'] as $key=>$ticket_num){
		$seat_data[$key] = array(
			'ticket_num' => $ticket_num,
			'area_id'    => $_REQUEST['c_area'][$key],
		);
	}
	unset($_REQUEST['c_pids'][$pid]);
}


$pid = $_REQUEST['pid'] + 0;
$aid = $_REQUEST['aid'] + 0;
$m   = new Member($s, $_SESSION['sid']);
$pro = new ProductInfo($s, $pid, $aid);
$disOrder = new DisOrder_H($s, $pro, $m);


if($pid==0) getOut("index.html");

// 基本信息检查
if(p_match($begintime=$_REQUEST['begintime']))
	exit("<script>alert('下单出错：游玩日期格式不正确');javascript:history.go(-1);</script>");
if(p_match($ordername=$_REQUEST['ordername']))
	exit("<script>alert('下单出错：取票姓名含有非法字符');javascript:history.go(-1);</script>");
if(p_match($ordertel=$_REQUEST['ordertel'])) 
	exit("<script>alert('下单出错：取票人含有非法字符');javascript:history.go(-1);</script>");
if(($tnum=$_REQUEST['tnum'] + 0)==0) 
	exit("<script>alert('下单出错：门票数量不能为零');javascript:history.go(-1);</script>");

$paymode=abs($_REQUEST['paymode']);
$totalTnum=$tnum;
foreach((array)$_REQUEST['c_pids'] as $c_pid=>$c_num){
	$totalTnum += $c_num;
}

$tourists=array();
$idcards=array();
for($n=1;$n<($totalTnum+1);$n++){
	if($_REQUEST['tourist_name_'.$n] && $_REQUEST['tourist_cert_'.$n]){
		$tourists[]=mysql_real_escape_string($_REQUEST['tourist_name_'.$n]);
		$idcards[]=mysql_real_escape_string($_REQUEST['tourist_cert_'.$n]);
	}
}

$option=array();
$option["pid"]         = $pid;
$option["tnum"]        = $tnum;
$option["c_pids"]      = $_REQUEST['c_pids'];
$option["ordertel"]    = $ordertel;
$option["begintime"]   = $begintime;
$option["leavetime"]   = $_REQUEST['leavetime'];
$option["ordername"]   = trim($ordername);
$option["remoteorder"] = $remoteorder;
$option["from"]        = $_REQUEST['from'];
$option['memo']        = mysql_real_escape_string($_REQUEST['memo']);
$option['origin']      = $_REQUEST['origin'];
$option['idcards']     = $idcards;
$option['paymode']     = $paymode;
$option['tourists']    = $tourists;
// $option['c_area']      = $_REQUEST['c_area'];
$option['seat_data']   = $seat_data;
$option['round_id']    = 114;
$option['venue_id']    = 3;

if(count($idcards)) $option['sfz'] = $idcards[0];
if(isset($_REQUEST['sfz'])) $option['sfz']=$_REQUEST['sfz'];


try{
	
	$r=$disOrder->order($option, $aid);
	write_logs(json_encode($r));
	$url="orderSuc.html?ordernum=".$r['orderNum'] . '&ot=' . md5($r['orderType']) ;
	if($_SESSION['dtype']==6){
		$daction="预订订单($r)";
		$str="insert pft_d_operation_rec set fid=".$_SESSION['memberID'].",sid=".$_SESSION['sid'].",rectime=now(),daction='$daction'";
		$GLOBALS['le']->query($str);
	}
}catch(DisOrderException $e){
	getOut("plist.html",$e->getMessage());
}catch(InterFaceException $e){
	$url="orderFailure.html?msg=".$e->getMessage()."：".$SoapRequestChk[$e->getCode()]."(".$e->getCode().")";
}catch(MsgException $e){
	$url="orderFailure.html?msg=".$e->getMessage()."：".telcode($e->getCode())."(".$e->getCode().")";
}
if($_REQUEST['saveTo']==1){//常用联系人存储
	$m->addCinfo($ordername,$ordertel);
}
header("Location:$url");
function getOut($url,$msg=null){
	$js="<script>";
	if($msg){ $js.="alert('$msg');" ;}
	$js.="window.location='$url'</script>";
	exit($js);
}

function write_logs($txt,$file="/var/www/html/new/d/logs/orderInfo_logs.txt"){
	$fp = fopen($file,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}
?>